package leetcode01_10;

import org.junit.Test;

import java.util.ArrayDeque;
import java.util.HashSet;
import java.util.Set;

/**
 * @author shhjiang_001
 * @create 2020-03-08 21:40
 */
public class Solution03 {
    public int lengthOfLongestSubstring1(String s) {
        Set set = new HashSet();
        int ans = 0, i = 0,j = 0;
        while(i<s.length() && j<s.length()){
            if (set.contains(s.charAt(j)) == false){
                set.add(s.charAt(j));
                j++;
                ans = Math.max(j-i,ans);
            }else{
                set.remove(s.charAt(i));
                i++;
            }
        }
        return ans;
    }
    public int lengthOfLongestSubstring(String s) {
        int res = 0;
        int ri = 0;
        int le = 0;
        int[] window = new int[128];
        while (ri < s.length()){
            char c = s.charAt(ri);
            ri++;
            window[c]++;
            while (window[c]>1){
                char d = s.charAt(le);
                le++;
                window[d]--;
            }
            res = Math.max(ri-le,res);
        }
        return res;
    }
    @Test
    public void test(){
        Solution03 solution03 = new Solution03();
        int i = solution03.lengthOfLongestSubstring("abcabcbb");
        System.out.println(i);
    }
}
